<?php


/**
 * @author Garry
 *
 *
 */
class Image extends HtmlDisplay {
	//TODO - Insert your code here
	public $finalPath="";

	public function __construct( $path,$maxsize=null,$originalwidth=null,$height=0,$dir_prefix="/thumbs_",$crop=false) {
		parent::__construct();
		$this->dat = $this->HtmlElement('img');
		$width=0;
		if( !empty($maxsize)){
			$controller = CI_Controller::get_instance();
			$controller->load->helper('Image_helper');
			if($crop){
				$path = resizeImage( $path,$maxsize,$maxsize,$dir_prefix.$maxsize,$crop );
				$width = $maxsize;
				$height = $maxsize;
			}
			else {
				if( $originalwidth >= $height && $originalwidth >= $maxsize  ){
					$ratio = $originalwidth/$height;
					$height = round($maxsize/$ratio);
					$width = $maxsize;
					$path = resizeImage( $path,$width,$height,$dir_prefix.$width,$crop );
				}
				else if( $height > $originalwidth && $height > $maxsize ){
					$ratio = $height/$originalwidth;
					$width = round($maxsize/$ratio);
					$height = $maxsize;
					$path = resizeImage( $path,$width,$maxsize,$dir_prefix.$maxsize,$crop );
				}
			}
			$this->dat->addAttribute('width',$width);
			$this->dat->addAttribute('height',$height);		
		}
		$this->dat->addAttribute('src',$path);
		$this->finalPath = $path;
	}
	/**
	 *
	 */
	function __destruct() {

		//TODO - Insert your code here
	}
	
	public function finalize(){
		$this->output = new stdClass();
		$this->output->imgs = (string)$this->cleanseXMLString($this->dat->asXML());
	}
	
}


?>